$(document).ready(function () {
    $.backend.selectAll();
    $(".to-auditor").click(function() {
        console.log("Start to ticket to auditor.");
        var auditor = $(this).attr("title");
        console.log("auditor", auditor);
        var selectIds = "";
        $('input[name=ids]:checked').each(function() {
            selectIds += $(this).val() + ",";
        });
        selectIds = selectIds.substring(0, selectIds.length - 1);
        console.log("selectIds:", selectIds);
        var url = "/admin/ticket/toAuditorAjax";
        var params = {
            "auditor": auditor,
            "ticketIds": selectIds
        };
        var messages = $.backend.sendAjax(url, params);
        console.debug("messages", messages);
        for(var i=0; i<messages.length; i++) {
            var message = messages[i];
            $.alert(message.message, message.level);
        }
        setTimeout(function(){
            $("#searchForm").submit();
        }, 2000);
    });
    
    //挂起ticket
    $(".suspend").click(function() {
        console.log("Start to suspend ticket");
        var selectIds = "";
        $('input[name=ids]:checked').each(function() {
            selectIds += $(this).val() + ",";
        });
        selectIds = selectIds.substring(0, selectIds.length - 1);
        console.log("selectIds:", selectIds);
        var url = "/admin/ticket/isSuspendAjax";
        var params = {
            "ticketIds": selectIds
        };
        var messages = $.backend.sendAjax(url, params);
        console.debug("messages", messages);
        for(var i=0; i<messages.length; i++) {
            var message = messages[i];
            $.alert(message.message, message.level);
        }
        setTimeout(function(){
            $("#searchForm").submit();
        }, 2000);
    });

    $(".query").click(function() {
        $("#pager_page").val(1); // 重置分页
    });



    $(".batch-pass").click(function() {
        console.debug("Start to batch pass ...");
        alert("批量审核耗时长，请耐心等待，结束后自动刷新");
        var checkedList = checkedCheckbox();
        var checkedIds = checkedList.map(function(){
            return $(this).val();
        }).get();
        var params = {
            "ticketIds": checkedIds
        };
        var url = "/admin/ticket/batchPass";
        var messages = $.backend.sendAjax(url, params);
        console.debug("messages", messages);
        for(var i=0; i<messages.length; i++) {
            var message = messages[i];
            $.alert(message.message, message.level);
        }

        console.debug("End batch pass ...");
        setTimeout(function(){
            $("#searchForm").submit();
        }, 2000);
    });

    $("#batch-refuse").live("click", function() {
        console.debug("Start to batch refuse ...");
        $("#batch-modal").fadeOut();
        var reason = $("#reason").val();
        var checkedList = checkedCheckbox();
        var selectIds = $.backend.linkIds(checkedList);
        var params = {
            "reason": reason,
            "ticketIds": selectIds
        };
        var url = "/admin/ticket/batchRefuse";
        var messages = $.backend.sendAjax(url, params);
        console.debug("messages", messages);
        for(var i=0; i<messages.length; i++) {
            var message = messages[i];
            $.alert(message.message, message.level);
        }

        setTimeout(function(){
            $("#searchForm").submit();
        }, 2000);
    });

    $(".batch-refuse-reason").click(function(){
        console.debug("Start to batch refuse ...");
        var checked = checkedCheckbox();
        if(checked.length > 0) {
            var content = "您选择了" + checked.length + "条记录，请输入批量拒绝原因";
            $("#batch-operate-title").html(content);
            $("#batch-modal").fadeIn();
        } else {
            $.alert("您没有选中任何条目。", "warning");
        }
        $(".batch-submit").attr("id", "batch-refuse");
        console.debug("End to batch refuse ...");
    });

    $(".batch-off-shelf").click(function(){
        console.debug("Start to batch off shelf ...");
        var checkedList = checkedCheckbox();
        var selectIds = $.backend.linkIds(checkedList);
        var params = {
            "ticketIds": selectIds
        };
        var url = "/admin/ticket/batchOffShelf";
        var messages = $.backend.sendAjax(url, params);
        console.debug("messages", messages);
        for(var i=0; i<messages.length; i++) {
            var message = messages[i];
            $.alert(message.message, message.level);
        }

        setTimeout(function(){
            $("#searchForm").submit();
        }, 2000);
        console.debug("End to batch off shelf ...");
    });
});

function checkedCheckbox() {
    return $(".selectOne:checked");
}